import axiosInstance from "@/store/apiClient";


const download_file = {
	get: async function (data) {
		try {
			let blob_list = [];
			let save_file_name = data.file_name;
			console.log(data)
			const response1 = await axiosInstance.post('/download', data, {
				responseType: 'blob', // 指定响应类型为 blob
			})
			if (response1.status !== 200 || !response1.data) {
				console.error("下载失败, data: ", response1.data);
				return;
			}
			blob_list.push(new Blob([response1.data]));

			const mergedBlob = new Blob(blob_list);
			// 创建对象 URL，生成下载链接
			const url = window.URL.createObjectURL(mergedBlob);
			// 创建链接和触发下载
			const a = document.createElement('a');
			a.href = url;
			// 这里设置下载的文件名
			a.download = `${save_file_name}`;
			document.body.appendChild(a);
			a.click();
			// 清理 DOM
			a.remove();
			window.URL.revokeObjectURL(url); // 释放占用的内存
		} catch (error) {
			console.error(error);
		}
	}
}

export default download_file